import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gp_print_util/GpBlueUtils.dart';
import 'package:gp_print_util/Utils.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  static const platform = const MethodChannel(Utils.receiptPrintMsg);
  static const EventChannel connectionPlatfrom =
      const EventChannel(Utils.MSG_BTCONNECT);

  GpBlueUtils deviceUtils = GpBlueUtils();

  _print() {
    deviceUtils.printReceipt({
      "orderId": "22222",
      "time": "08-05 13:31",
      "name": "凣老弟",
      "phone": "13412345678",
      "dishName": "香巴佬牛肉饭",
      "dishId": "11111",
      "remark": "清凉雪碧+肥宅快乐水+bbq牛排意面通心粉，红烧油闷臭豆腐，软香披萨番茄汁。鲍鱼大虾加啤酒",
      "resName": "易爱体验店",
      "id": 123213123,
      "amount": 10.01
    });
  }

  _connection() {
    deviceUtils.connectBt("DC:0D:30:22:66:4B");
    deviceUtils.connectStateListener(
      (event) => print("value =  $event"),
    );
  }

  _checkIsEmable() async {
    var flag = await deviceUtils.checkBtEnable();
    print("蓝牙状态 = $flag");
  }

  _deviceList() {
    if (Platform.isAndroid) {
      platform.invokeMethod("searchBTDevices");
      connectionPlatfrom.receiveBroadcastStream().listen((event) {
        // {code: 1152, address: DC:0D:30:22:66:4B}
        print("value =  $event");
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text("测试"),
        ),
        body: Column(
          children: [
            Row(
              children: [
                RaisedButton(
                  onPressed: () => _connection(),
                  child: Text("连接"),
                ),
                RaisedButton(
                  onPressed: () => _print(),
                  child: Text("测试"),
                ),
                RaisedButton(
                  onPressed: () => _deviceList(),
                  child: Text("蓝牙"),
                ),
              ],
            ),
            Row(
              children: [
                RaisedButton(
                  onPressed: () => _checkIsEmable(),
                  child: Text("蓝牙是否打开"),
                ),
                // RaisedButton(
                //   onPressed: () => _print(),
                //   child: Text("测试"),
                // ),
                // RaisedButton(
                //   onPressed: () => _deviceList(),
                //   child: Text("蓝牙"),
                // ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
